/*!
* @author @HuntBao
* @project list view
*/
App.Views.projectView = App.Views.leftMenuItemView.extend({
    
    initialize: function() {
        var self = this;
        self.model.bind('change:name', self.nameChangeHandler, self);
        self.model.bind('change:uiOpen', self.openChangeHandler, self);
        self.model.bind('change:count', self.countChangeHandler, self);
        self.model.bind('change:hasFolder', self.hasFolderChangeHandler, self);
        self.model.bind('destroy', self.destroyHandler, self);
        self.model.bind('remove', self.removeHandler, self);
    },
    
    render: function(){
        var self = this,
        model = self.model;
        model.set({uiUrl: '#!myProject&id=' + model.get('id')}, {silent: true});
        if(model.get('projectId') == model.get('parentId')){
            model.set({iconCls: model.get('creator') === App.userId ? 'user-self-icon' : 'user-other-icon'}, {silent: true});
        }else{
        	model.set({iconCls: model.get('uiOpen') ? 'folder-icon icon-folder-open' : 'folder-icon icon-folder-close'}, {silent: true});
        }
        model.set({uiName: model.get('name')}, {silent: true});
        self.$el.html(Mustache.to_html(self.template, model.toJSON())).attr('title', model.get('uiName'));
        var privileges = model.get('attributes').privileges;
        if(privileges && privileges.length !== 4){
            self.$el.addClass('no-dropdown-menu');
        }
        self.toggleIcon = self.$el.find('.toggle-icon');
        self.folderIcon = self.$el.find('.folder-icon');
        self.uiName = self.$el.find('.menu-text');
        self.badge = self.$el.find('.badge');
        return self;
    },
    
    nameChangeHandler: function(){
        var self = this;
        self.uiName.text(self.model.get('name'));
        var breadcrumbId = self.model.get('id');
        var breadcrumbModel = App.viewInstances.appView.breadcrumbCollection.get(breadcrumbId);
        if(breadcrumbModel){
            breadcrumbModel.set('name', self.model.get('name'));
        }
    },
    
    openChangeHandler: function(){
        var self = this;
        if(self.model.get('uiOpen')){
            self.toggleIcon.addClass('toggle-icon-expand');
            self.folderIcon.removeClass('icon-folder-close').addClass('icon-folder-open');
            self.$el.children('.sub-nav-list').slideDown();
        }else{
            self.toggleIcon.removeClass('toggle-icon-expand');
            self.folderIcon.removeClass('icon-folder-open').addClass('icon-folder-close');
            self.$el.children('.sub-nav-list').slideUp();
        }
        return self;
    },
    
    countChangeHandler: function(){
        var self = this;
        self.badge.text(self.model.get('count'));
    },
    
    hasFolderChangeHandler: function(){
        var self = this;
        if(self.model.get('hasFolder')){
            self.toggleIcon.addClass('toggle-icon-show');
        }else{
            self.toggleIcon.removeClass('toggle-icon-show');
        }
    },
    
    destroyHandler: function(model, collection, options){
        var self = this;
        self.$el.slideUp(function(){
        	var t = $(this);
        	if(t.next().length > 0){
        	   t.next().find('.url')[0].click();
        	}else{
        	   var first = t.parent().children().eq(0);
        	   if(first.length > 0 && !first.is(t)){
        	       first.find('.url')[0].click();
        	   }else{
        	       //no project exist
        	   	   var appView = App.viewInstances.appView;
        	   	   appView.noProjectTip.fadeIn();
        	   	   //select ownMe
        	   	   $('#' + App.util.getIdWithPrefix(appView.leftMenuIds.ownMe)).find('.url')[0].click();
        	   }
        	}
            t.remove();
        });
    },
    
    removeHandler: function(model, collection, options){
        var self = this;
        $('#' + App.util.getIdWithPrefix(model.get('id'))).slideUp();
        var parentFolder = $('.' + App.util.getIdWithPrefix(model.get('parentId')));
        if(parentFolder.length === 1){
            var parentFolderModel = App.viewInstances.appView.getProjectItemModelById(parentFolder.attr('id'));
            var count = parentFolderModel.get('count');
            parentFolderModel.set({
                count: count - 1
            });
        }
    }
});
